package com.css.dzj.dzjap.modules.info.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.construnit.entity.ConstructionUnitEntity;
import com.css.dzj.dzjap.modules.construnit.service.ConstrunitService;
import com.css.dzj.dzjap.modules.info.service.ConstrService;
import com.css.dzj.dzjap.modules.info.vo.EvalInfoVo;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 建设单位
 * @author liangxingguo
 */
@RestController
@RequestMapping("/constr")
public class ConstrController {

        @Autowired
        ConstrService constrService;
        @Autowired
        ConstrunitService construnitService;
        @Autowired
        BaseService baseService;
    /***
     * 分页条件查询
     * @param pageNo
     * @param pageSize
     * @param entName
     * @param createPerson
     * @return
     */
        @GetMapping("/listpage")
        public R getPageContr(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                              @RequestParam(name = "entName", required = false) String entName,
                              @RequestParam(name = "uniscid", required = false) String uniscid,
                              @RequestParam(name = "startTime", required = false) String startTime,
                              @RequestParam(name = "endTime",required = false) String endTime,
                              @RequestParam(name = "createPerson", required = false) String createPerson, HttpServletRequest request){
            PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
            QueryResults page=constrService.getPage(pageRequest,entName,uniscid,startTime,endTime,createPerson,request);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("page",page);
        }

    /**
     * 分页查询建设单位
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/listcontr")
    public R getContr(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "enterpriseName", required = false) String enterpriseName,
                     @RequestParam(name = "enterpriseIdNumber", required = false) String enterpriseIdNumber){
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults page=constrService.getPageConstr(pageRequest,enterpriseName,enterpriseIdNumber);
        List<ConstructionUnitEntity> list= page.getResults();
        List<EvalInfoVo> evalInfoVoList=new ArrayList<>();
        for (ConstructionUnitEntity constructionUnitEntity : list) {
            EvalInfoVo evalInfoVo=new EvalInfoVo();
            evalInfoVo.setEnterpriseName(constructionUnitEntity.getEntName());
            evalInfoVo.setEnterpriseIdNumber(constructionUnitEntity.getUniscid());
            evalInfoVo.setEnterpriseIdType(constructionUnitEntity.getEntType());
            evalInfoVo.setLegalMan(constructionUnitEntity.getLegalPerson());
            evalInfoVo.setRecordUnitUser(constructionUnitEntity.getLinkMan());
            evalInfoVo.setRecordUnitPhone(constructionUnitEntity.getLinkTel());
            evalInfoVoList.add(evalInfoVo);
        }
        QueryResults queryResults=new QueryResults(evalInfoVoList,page.getTotal(),page.getOffset(),page.getTotal());
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",queryResults);
    }


    /***
     * 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/delConstr/{id}")
        public R delConstr(@PathVariable String id){
           boolean flag= constrService.delConstr(id);
           if (flag){
               baseService.logSuccess(3,"删除成功","2");
               return R.ok("删除成功");
           }
        baseService.logFailed(3,"删除失败","2");
          return R.error("删除失败");
        }

    /***
     * 更新建设单位
     * @param constructionUnitEntity
     * @return
     */
        @PostMapping("/update")
        public R updateConstr(@RequestBody ConstructionUnitEntity constructionUnitEntity){
            try{

                ConstructionUnitEntity constructionEntity=constrService.findById(constructionUnitEntity.getId());
                BeanUtil.copyBeanNotEmptyToBean(constructionUnitEntity,constructionEntity);
                constructionEntity.setUdateTime(new Date());
                constrService.update(constructionEntity);
                baseService.logSuccess(3,"更新成功","2");
                return R.ok();
            }catch (Exception e){
                baseService.logFailed(3,"更新失败","2");
                return R.error(e.getMessage());
            }
        }

    /***
     * 根据ID查询建设单位
     * @param id
     * @return
     */
    @PostMapping("/findbyid/{id}")
    public R findById(@PathVariable String id){
        if(!StringUtils.isEmpty(id)) {
            ConstructionUnitEntity constructionUnitEntity = constrService.findById(id);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("constructionUnitEntity", constructionUnitEntity);
        }else {
            baseService.logFailed(3,"查询成功","2");
            return R.error("id为空");
        }
    }

    /***
     * 添加建筑单位
     * @param constructionUnitEntity
     * @return
     */
    @PostMapping("/saveconstr")
    public R save(@RequestBody ConstructionUnitEntity constructionUnitEntity){
       try {
           // 判断企业名称是否已存在
           boolean isExistUserName = construnitService.isExistUserName(constructionUnitEntity.getUserName());
           if(isExistUserName) {
               return R.error("企业名称已存在!");
           }
           // 判断统一社会信用代码是否已存在
           boolean isExistEntCode = construnitService.isExistEntCode(constructionUnitEntity.getUniscid());
           if(isExistEntCode) {
               return R.error("统一社会信用代码已存在!");
           }

           construnitService.regist(constructionUnitEntity);
           baseService.logSuccess(3,"添加成功","2");
           return R.ok("添加成功");
       }catch (Exception e){
           baseService.logFailed(3,"添加失败","2");
           return R.error(e.getMessage());
       }
    }
    /***
     * 获取建设单位
     * @return
     */
    @GetMapping("/countconstr")
    public R countconstr(){
        HashMap count=constrService.getCount();
        return R.ok().put("count",count);
    }
    /***
     * 获取建设单位根据年度
     * @return
     */
    @GetMapping("/countconstryear")
    public R countconstrByYear(String year){
        HashMap count=constrService.getCountByYear(year);
        return R.ok().put("count",count);
    }
}
